import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class SiteAccountLogService extends BaseService<any> {
  private readonly logger = new Logger(SiteAccountLogService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * getPage
   * 对应 PHP: SiteAccountLogService_admin::getPage()
   * 逻辑类型: undefined - undefined
   */
  async getPage(where: any[]) {
    // 基于PHP真实逻辑: getPage
    // PHP原文: $field = 'id, site_id, type, money, trade_no, create_time';         $search_model = $this->model->where([ [ 'site_id', '=', $this->site_id ] ])->withS...
const field = 'id, site_id, type, money, trade_no, create_time';
        const search_model = this.model.where([ [ 'site_id', '=', this.site_id ) )).withSearch([ 'create_time', 'type', 'trade_no' ), where).field(field).append([ 'type_name', 'pay_info', 'money' )).order('create_time desc'];
        return this.pageQuery(search_model);
    }

    /**
     * 获取账单详情
     * @param int id
     * @return */
    async getInfo(int id)
    {
        const field = 'id, site_id, type, money, trade_no, create_time';
        return this.model.where([ [ 'site_id', '=', this.site_id ], ['id', '=', id))).field(field).append([ 'type_name', 'pay_info' )).findOrEmpty().toArray(];

    }

    /**
     * 统计数据
     * @return */
    async stat()
    {
        return [
            pay: this.model.where([[ 'site_id', '=', this.site_id ], ['type', '=', 'pay'))).sum("money")*1,
            refund: this.model.where([[ 'site_id', '=', this.site_id ], ['type', '=', 'refund'))).sum("money")*-1,
            transfer: this.model.where([[ 'site_id', '=', this.site_id ], ['type', '=', 'transfer'))).sum("money")*-1,
        ;
  }
  }

  /**
   * getInfo
   * 对应 PHP: SiteAccountLogService_admin::getInfo()
   * 逻辑类型: undefined - undefined
   */
  async getInfo(id: number) {
    // 基于PHP真实逻辑: getInfo
    // PHP原文: $field = 'id, site_id, type, money, trade_no, create_time';         return $this->model->where([ [ 'site_id', '=', $this->site_id ], ['id', '=', $id]]...
const field = 'id, site_id, type, money, trade_no, create_time';
        return this.model.where([ [ 'site_id', '=', this.site_id ], ['id', '=', id))).field(field).append([ 'type_name', 'pay_info' )).findOrEmpty().toArray(];

    }

    /**
     * 统计数据
     * @return */
    async stat()
    {
        return [
            pay: this.model.where([[ 'site_id', '=', this.site_id ], ['type', '=', 'pay'))).sum("money")*1,
            refund: this.model.where([[ 'site_id', '=', this.site_id ], ['type', '=', 'refund'))).sum("money")*-1,
            transfer: this.model.where([[ 'site_id', '=', this.site_id ], ['type', '=', 'transfer'))).sum("money")*-1,
        ;
  }
  }

  /**
   * stat
   * 对应 PHP: SiteAccountLogService_admin::stat()
   * 逻辑类型: undefined - undefined
   */
  async stat() {
    // 基于PHP真实逻辑: stat
    // PHP原文: return [             'pay' => $this->model->where([[ 'site_id', '=', $this->site_id ], ['type', '=', 'pay']])->sum("money")*1,             'refund' =>...
return [
            pay: this.model.where([[ 'site_id', '=', this.site_id ], ['type', '=', 'pay'))).sum("money")*1,
            refund: this.model.where([[ 'site_id', '=', this.site_id ], ['type', '=', 'refund'))).sum("money")*-1,
            transfer: this.model.where([[ 'site_id', '=', this.site_id ], ['type', '=', 'transfer'))).sum("money")*-1,
        ;
  }
  }
}
